package com.wlgc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.wlgc.dao.UserDao;
import com.wlgc.entity.RestBean;
import com.wlgc.entity.User;
import com.wlgc.service.RegisterService;
import com.wlgc.service.UpdateUserService;
import com.wlgc.service.VerifyAndRegisterService;
import com.wlgc.service.LoginService;
import com.wlgc.service.impl.UserServiceImpl;
import com.wlgc.util.RedisCache;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@RestController
@RequestMapping("/auth/verify")
public class AuthController {

    @Resource
    RedisCache redisCache;
    @Resource
    VerifyAndRegisterService verifyService;
    @Resource
    RegisterService registerService;
    @Resource
    LoginService loginService;
    @Resource
    UpdateUserService updateUserService;
    @Resource
    UserServiceImpl userService;


    @PostMapping("/register")
    //注册账号
    RestBean register(@RequestParam("email") String email,
                      @RequestParam("verifyCode") String verifyCode, @RequestParam("username") String username,
                      @RequestParam("password") String password, @RequestParam("id") String id) {

        if (id.isEmpty()) return new RestBean(500, "请输入学号");
        if (username.isEmpty()) return new RestBean(500, "请输入用户名！");
        if (email.isEmpty()) return new RestBean(500, "请输入邮箱");
        if (verifyCode.isEmpty()) return new RestBean(500, "请输入验证码");
        if (password.isEmpty()) return new RestBean(500, "请输入密码");


        if (verifyService.verify(email, verifyCode)) {
            //验证码正确，则进行注册,注册成功在service内使验证码失效
            return registerService.register(Integer.parseInt(id), username, password, email);
        } else
            return new RestBean(500, "验证码错误");
    }

    @PostMapping("/send-code")
    //向邮箱发送验证码，根据refer判断发送所处条件
    RestBean sendVerifyCode(@RequestParam("email") String email, @RequestParam("refer") String refer) {

        return verifyService.sendVerifyCode(email, refer);


    }

    @PostMapping("/login")
    //用用户名登录
    RestBean login(@RequestParam("username") String username, @RequestParam("password") String password) {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        return loginService.login(user);
    }

    @PostMapping("/login-by-id")
    //用学号登录
    RestBean loginById(@RequestParam("id") String id, @RequestParam("password") String password) {
        User user = userService.getUserById(id);
        System.out.println(user.getUsername());
        user.setPassword(password);
        return loginService.login(user);
    }

    @GetMapping("/logout")
    //退出登录
    public RestBean logoutSuccess(HttpServletRequest request, HttpServletResponse response) {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();

        if (auth != null) {

            new SecurityContextLogoutHandler().logout(request, response, auth);
            //获取学号，并根据id删除学生信息
            redisCache.deleteObject("login:" + userService.getUserByUsername(auth.getName()).getId());

        }
        return new RestBean(200, "退出成功！");//You can redirect wherever you want, but generally it's a good practice to show login screen again.
    }

    @PostMapping("/update")
    //修改密码
    RestBean updateUser(@RequestParam("verifyCode") String verifyCode, @RequestParam("username") String username,
                        @RequestParam("password") String password, @RequestParam("email") String email,
                        @RequestParam("id") String id) {
        if (id.isEmpty()) return new RestBean(500, "请输入学号");
        if (username.isEmpty()) return new RestBean(500, "请输入用户名！");
        if (email.isEmpty()) return new RestBean(500, "请输入邮箱");
        if (verifyCode.isEmpty()) return new RestBean(500, "请输入验证码");
        if (password.isEmpty()) return new RestBean(500, "请输入密码");

        if (verifyService.verify(email, verifyCode)) {
            return updateUserService.updateUser(Integer.parseInt(id), username, password, email);
        } else {
            return new RestBean<>(500, "验证码错误！");
        }
    }
}
